﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Smith.ImageConverter
{
    public static class Provider
    {
        public static bool IsDeleteSource()
        {
            return Properties.Settings.Default.DeleteSource;
        }

        public static ConvertType GetDefaultTargetType()
        {
            return (ConvertType)Properties.Settings.Default.ConvertType;
        }

        public static string GetDefaultOutputDirectory()
        {
            OutputOption option = (OutputOption)Properties.Settings.Default.OutputOption;

            if (option == OutputOption.InDirectory)
            {
                string outdir = Properties.Settings.Default.OutputDirectory;
                if (string.IsNullOrEmpty(outdir)) return Environment.CurrentDirectory;
                else return new System.IO.DirectoryInfo(outdir).FullName;
            }
            else
            {
                return null;
            }
        }

        public static SerialGenerator GetDefaultGenerator()
        {
            return new SerialGenerator(
                Properties.Settings.Default.RenamePrefix,
                Properties.Settings.Default.RenameSuffix,
                Convert.ToInt32(Properties.Settings.Default.RenameSeed),
                Convert.ToInt32(Properties.Settings.Default.RenameDigit));
        }

        public static ResampleOption GetDefaultResampleMode()
        {
            return (ResampleOption)Properties.Settings.Default.ResampleOption;
        }

        public static IConvert GetDefaultConvert()
        {
            ConvertType type = (ConvertType)Properties.Settings.Default.ConvertType;

            switch (type)
            {
                case ConvertType.PNG:
                    return new PNGConvert();
                case ConvertType.BMP:
                    return new BMPConvert();
                default:
                    if (Properties.Settings.Default.DoCompress)
                        return new JPEGQConvert((long)(Properties.Settings.Default.CompressQuarity));
                    else return new JPEGDConvert();
            }
        }

        public static IResize GetDefaultResize()
        {
            ResizeOption option = (ResizeOption)Properties.Settings.Default.ResizeOption;
            IResize resize = new BaseResize();
            switch (option)
            {
                case ResizeOption.Wise:
                    resize = new WiseResize(
                        Convert.ToInt32(Properties.Settings.Default.WiseSideLength));
                    break;
                case ResizeOption.Width:
                    resize = new WidthResize(
                        Convert.ToInt32(Properties.Settings.Default.SideLength));
                    break;
                case ResizeOption.Height:
                    resize = new HeightResize(
                        Convert.ToInt32(Properties.Settings.Default.SideLength));
                    break;
                case ResizeOption.Customized:
                    resize = new CustomizedResize(
                        Convert.ToInt32(Properties.Settings.Default.CustomizeWidth),
                        Convert.ToInt32(Properties.Settings.Default.CustomizeHeight));
                    break;
                case ResizeOption.Percentage:
                    resize = new PercentageResize(
                        Properties.Settings.Default.ResizePercentage / 100.0);
                    break;
                default:
                    break;
            }
            resize.ResampleMode = GetDefaultResampleMode();
            return resize;
        }

        public static IRename GetDefaultRename()
        {
            RenameOption option = (RenameOption)Properties.Settings.Default.RenameOption;
            IRename rename = new BaseRename();
            if (option == RenameOption.Customized)
            {
                rename = new CustomizedRename(GetDefaultGenerator());
            }
            rename.TargetType = GetDefaultTargetType();
            return rename;
        }

        public static IRotation GetDefaultRotation()
        {
            RotationAngle angel = (RotationAngle)Properties.Settings.Default.RotationAngel;
            RotationFilter filter = (RotationFilter)Properties.Settings.Default.RotationFilter;
            IRotation rotation = new BaseRotation();
            switch (filter)
            {
                case RotationFilter.AllImages:
                    rotation = new AllRotation();
                    break;
                case RotationFilter.HorizontalImages:
                    rotation = new HorizontalRotation();
                    break;
                case RotationFilter.VerticalImages:
                    rotation = new VerticalRotation();
                    break;
                default:
                    break;
            }
            rotation.RotationAngle = angel;
            rotation.ResampleMode = GetDefaultResampleMode();
            return rotation;
        }
    }
}
